﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Flipmind.Xero;

namespace XeroInvoiceLoaderApp
{
    public partial class EditInvoiceLineItemForm : Form
    {
        public EditInvoiceLineItemForm()
        {
            InitializeComponent();
        }

        private TrackingCategory[] tracking;

        public InvoiceLineItem ShowForm(XeroAPI api, InvoiceLineItem curItem)
        {
            LoadDropdowns(api);
            txtAccountCode.Text = curItem.AccountCode;
            txtDescription.Text = curItem.Description;
            txtLineAmount.Text = curItem.LineAmount.ToString();
            txtQuantity.Text = curItem.Quantity.ToString();
            txtUnitAmount.Text = curItem.UnitAmount.ToString();
            txtTaxAmount.Text = curItem.TaxAmount.ToString();
            ddTaxType.SelectedItem = curItem.TaxType.ToString();
            if (curItem.Tracking != null)
            {
                ddTracking.SelectedItem = curItem.Tracking.Name;
                ddTrackingOption.SelectedItem = curItem.Tracking.Option;
            }
            
            ShowDialog();   
            return item;
        }

        private void LoadDropdowns(XeroAPI api)
        {
            string[] names = Enum.GetNames(typeof(TaxType));
            ddTaxType.Items.AddRange(names);

            tracking = api.GetTracking();
            foreach (TrackingCategory track in tracking)
            {
                ddTracking.Items.Add(track.Name);
            }
        }

        public InvoiceLineItem ShowForm(XeroAPI api)
        {
            LoadDropdowns(api);
            ShowDialog();            
            return item;
        }

        InvoiceLineItem item;

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                item = new InvoiceLineItem();
                item.AccountCode = txtAccountCode.Text;
                item.Description = txtDescription.Text;
                item.LineAmount = Decimal.Parse(txtLineAmount.Text);
                item.Quantity = Decimal.Parse(txtQuantity.Text);
                item.TaxType = (TaxType) Enum.Parse(typeof(TaxType), (string) ddTaxType.SelectedItem);
                item.UnitAmount = Decimal.Parse(txtUnitAmount.Text);
                item.TaxAmount = Decimal.Parse(txtTaxAmount.Text);

                if (ddTracking.SelectedIndex > -1)
                {
                    TrackingCategory track = tracking[ddTracking.SelectedIndex];
                    item.Tracking = new InvoiceLineItemTrackingCategory(track.Name, (string) ddTrackingOption.Items[ddTrackingOption.SelectedIndex]);    
                }
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem: " + ex);
            }            
        }

        private void ddTracking_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddTracking.SelectedIndex > -1)
            {
                ddTrackingOption.Items.Clear();
                TrackingCategory track = tracking[ddTracking.SelectedIndex];
                foreach (TrackingCategoryOption opt in track.Options)
                {
                    ddTrackingOption.Items.Add(opt.Name);
                }
            }            
        }
    }
}
